class Backend::AttachmentsController < ApplicationController

  def index
    @attachments = Attachment.select(:id, :file).order('id DESC').page(params[:page]).per(params[:per])
    render json: @attachments.map { |attach|
      {id: attach.id, url: attach.file.url, thumb: attach.file.url(:thumb)}
    }.to_json
  end

  def create
    @attachment = Attachment.new file: params[:file]
    @attachment.save
  end

  def destroy
    @attachment = Attachment.find params[:id]
    @attachment.destroy
  end

  def info
    @total_pages = Attachment.count / 12
    render json: {total_pages: @total_pages}
  end
end
